<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>给指定的class添加class</title>
</head>

<body>
  js的classList:https://blog.csdn.net/qq_52845451/article/details/125665560
  <div class="style_class">简单的内容</div>
  <button onclick="btn()">添加</button>
  <button onclick="btnOne()">设置为新的</button>
  <button onclick="btnTwo()">在原来的后面加这个</button>
  <script>
    const idClass = document.querySelector(".style_class");
    //设置为新的
    const btn = () => idClass.classList.add("active");
    const btnOne = () => idClass.className = 'active'; //设置为新的
    const btnTwo = () => idClass.className += ' active'; //在原来的后面加这个
  </script>
  <style>
    .style_class {
      font-size: 12px;
    }

    .active {
      background-color: blueviolet;
    }
  </style>
</body>

</html>